package com.youlu.campus.web.leads.service.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.leads.entity.SalesLeads;
import com.youlu.campus.web.leads.entity.TeacherInfo;
import com.youlu.campus.web.leads.service.SalesLeadsService;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.web.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

@Service
@Slf4j
public class SalesLeadsServiceImpl extends BaseService implements SalesLeadsService {
    @Autowired
    private UserService userService;

    @Override
    public Boolean create(SalesLeads s) {
        log.info(":>>> 开始创建销售信息:{}", JSON.toJSONString(s));
        if (Objects.isNull(s) || StringUtils.isBlank(s.getClassification())
                || StringUtils.isBlank(s.getTeacherId())) {
            log.error(":>>> 创建销售线索参数错误");
            throw new BusinessException("参数错误!");
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(s.getTeacherId()));
        TeacherInfo teacher = mongoTemplate.findOne(query, TeacherInfo.class);
        if (Objects.isNull(teacher)) {
            log.error(":><<< 老师信息:{} 不存在", s.getTeacherId());
            throw new BusinessException("老师信息不存在请检查!");
        }
        s.setId(null);
        s.setTeacherId(s.getTeacherId());
        s.setTeacherDescription(teacher.getDescription());
        s.setTeacherImageUrl(teacher.getImageUr());
        s.setTeacherName(teacher.getName());
        s.setCreatedTime(new Date());

        UserPO userPO = userService.queryUserById(s.getUserId());
        if (Objects.isNull(userPO)) {
            log.error(":><<< 用户信息:{} 不存在", s.getUserId());
            throw new BusinessException("用户信息不存在请检查!");
        }
        s.setUserId(s.getUserId());
        s.setUserImageUrl(userPO.getAvatarUrl());
        s.setGender(userPO.getGender());
        s.setNickName(userPO.getNickName());
        s.setMobile(userPO.getMobile());
        s.setCreatedTime(new Date());
        SalesLeads r = mongoTemplate.insert(s);
        if (Objects.nonNull(r)) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

    @Override
    public Page<TeacherInfo> listTeachers(TeacherInfo teacherInfo, Integer page, Integer pageSize) {
        log.info(":>>> 获取教师信息:{},page:{},pageSie:{}", JSON.toJSONString(teacherInfo), page, pageSize);
        Query query = new Query();
        if (Objects.nonNull(teacherInfo)) {
            if (Objects.nonNull(teacherInfo.getClassification())) {
                query.addCriteria(Criteria.where("classification").is(teacherInfo.getClassification()));
            }
            if (StringUtils.isNotBlank(teacherInfo.getName())) {
                Pattern pattern = Pattern.compile("^.*" + teacherInfo.getName() + ".*$", Pattern.CASE_INSENSITIVE);
                query.addCriteria(Criteria.where("name").regex(pattern));
            }
        }
        long count = mongoTemplate.count(query, TeacherInfo.class);
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);

//        query.with(new Sort(Sort.Direction.DESC, "weight"));
        Sort sort=Sort.by(Sort.Direction.DESC, "weight");
        query.with(sort);
        // TODO:
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        List<TeacherInfo> datas = mongoTemplate.find(query.with(pageRequest), TeacherInfo.class);
        return PageableExecutionUtils.getPage(datas, pageRequest, () -> count);
    }
}
